package com.platform.enums;

import com.google.common.base.Strings;
import com.google.common.collect.Maps;
import lombok.Getter;

import java.util.Map;
import java.util.stream.Stream;

/**
 * Description: 自然人证件类型
 *
 * @author libin created on 2019/9/25
 */
public enum NatIdType {
    /**
     * 证件编码
     */
    SFZ("N1", "id_sfz", "身份证"),
    HZH("N2", "id_hzh", "护照号"),
    GOTXZ("N3", "id_go_txz", "港澳居民来往内地通行证"),
    TWTZX("N4", "id_tw_txz", "台湾居民来往大陆通行证"),
    WGRCODE("N5", "id_wgr_code", "外国人永久居留身份证"),
    OTHER("99", "id_other", "其他");

    /**
     * 编码-对应字典编码
     */
    @Getter
    private final String code;

    /**
     * 字段名称 - 对应元信息字段名
     */
    @Getter
    private final String name;

    /**
     * 名称
     */
    @Getter
    private final String text;

    NatIdType(String code, String name, String text) {
        this.code = code;
        this.name = name;
        this.text = text;
    }

    public static NatIdType getByCode(String code) {
        if (SFZ.getCode().equals(code)) {
            return SFZ;
        } else if (HZH.getCode().equals(code)) {
            return HZH;
        } else if (GOTXZ.getCode().equals(code)) {
            return GOTXZ;
        } else if (TWTZX.getCode().equals(code)) {
            return TWTZX;
        } else if (WGRCODE.getCode().equals(code)) {
            return WGRCODE;
        } else {
            return OTHER;
        }
    }

    public static String getCodeByText(String text) {
        return Stream.of(values())
                .filter(natIdType -> natIdType.is(text))
                .findFirst()
                .map(NatIdType::getCode)
                .orElse(null);
    }

    public static String getNameByCode(String code) {
        return getByCode(code).getName();
    }

    public static Map<String, Object> getNameByCode(String code, Object val) {
        Map<String, Object> map = Maps.newLinkedHashMap();
        Stream.of(values()).forEach(id -> map.put(id.getName(), null));
        map.put(getNameByCode(code), val);
        return map;
    }

    public boolean is(String text) {
        if (Strings.isNullOrEmpty(text)) {
            return false;
        }
        return getText().equals(text);
    }
}
